package com.ttai.ui.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerRegisterComponet;
import com.ttai.dagger.module.activity.RegisterPresenterModule;
import com.ttai.presenter.activity.RegisterPresenter;
import com.ttai.presenter.activity.SigninPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.ui.other.RegisterDialog;
import com.ttai.untils.Constant;
import com.umeng.analytics.MobclickAgent;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Register extends BaseActivity {
    @Bind(R.id.et_registerphone)
    EditText etRegisterphone;
    @Bind(R.id.et_registercode)
    EditText etRegistercode;
    @Bind(R.id.btn_resisterget)
    Button btnResisterget;
    //    @Bind(R.id.et_registeremail)
//    EditText etRegisteremail;
//    @Bind(R.id.switchButton)
//    SwitchButton switchButton;
    @Bind(R.id.btn_finsh)
    Button btnFinsh;
    @Bind(R.id.cb_controller)
    CheckBox cbController;
    @Bind(R.id.btn_controller)
    Button btnController;
    @Bind(R.id.cb_zhiwen)
    CheckBox cbZhiwen;
    @Bind(R.id.iv_finger)
    ImageView ivFinger;

    @Inject
    RegisterPresenter registerPresenter;

    SigninPresenter signinPresenter;
    RegisterDialog registerDialog;

    public static String phonenumber = "";


    /**
     * 计时器
     */
    private CountDownTimer timer = new CountDownTimer(30000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            btnResisterget.setEnabled(false);
            btnResisterget.setText("休息" + (millisUntilFinished / 1000) + "秒");
        }

        @Override
        public void onFinish() {
            btnResisterget.setEnabled(true);
            btnResisterget.setText("获取验证码");
        }
    };
    private String returnData = "0";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView) findViewById(R.id.barTitle);
        barTitle.setText("选择我们，就是选择安全");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        cbZhiwen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("00", "onClick: satus:"+cbZhiwen.isChecked());
                if (cbZhiwen.isChecked() == true){
                    Intent intent = new Intent(Register.this, ZhiWen.class);
                    intent.putExtra("value", "register");
//                    startActivity(intent);
                    startActivityForResult(intent, 1);
                }else {
                    ivFinger.setBackgroundResource(R.drawable.register_fingerprint_no);
                    cbZhiwen.setChecked(false);
                }
            }
        });



        signinPresenter = new SigninPresenter(this);
        setComponet();

        btnController.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
    }


    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("注册界面"); //手动统计页面("SplashScreen"为页面名称，可自定义)，必须保证 onPageEnd 在 onPause 之前调用，因为SDK会在 onPause 中保存onPageEnd统计到的页面数据。
        MobclickAgent.onPause(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("注册界面"); //手动统计页面("SplashScreen"为页面名称，可自定义)
        MobclickAgent.onResume(this); //统计时长
        if (returnData.equals("0")){
            cbZhiwen.setChecked(false);
        }
    }

    @Override
    public void setComponet() {
        DaggerRegisterComponet.builder()
                .registerPresenterModule(new RegisterPresenterModule(this))
                .build()
                .in(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {  // 先对应请求码
            case 1:
                if (resultCode == RESULT_OK) {  // 根据结果值来确定是否输出
                    returnData = data.getStringExtra("return_data");
                    System.out.println("returnData:" + returnData);
                    if (returnData.equals("1")) {
                        ivFinger.setBackgroundResource(R.drawable.register_fingerprint);
                        cbZhiwen.setChecked(true);
                    }
                }
                break;
            default:
        }
    }


    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }


    @OnClick({R.id.btn_resisterget, R.id.btn_finsh, R.id.btn_controller})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_resisterget:
                phonenumber = etRegisterphone.getText().toString();
                /**
                 * 判断是否为手机号
                 */

                String mac = Constant.getMacAddress();

                if (!Constant.isMobile(phonenumber)) {
                    Toast.makeText(Register.this, "输入的手机号无效", Toast.LENGTH_SHORT).show();
                } else {
                    /**
                     * 启动计时器
                     */
                    timer.start();
                }

                registerPresenter.getVerifyCode(phonenumber, "0");

                break;
            case R.id.btn_finsh:
                if (cbController.isChecked() == false) {
                    Toast.makeText(Register.this, "请先同意免责协议", Toast.LENGTH_SHORT).show();
                }else if (cbZhiwen.isChecked() == false) {
                    Toast.makeText(Register.this, "请先勾选指纹验证", Toast.LENGTH_SHORT).show();
                }else {
                    try {
                        String scode = getScode(etRegistercode.getText().toString() + registerPresenter.buildTime.getBuildtime());
                        String clientId = PushManager.getInstance().getClientid(this);
//                String email = etRegisteremail.getText().toString();
                        String email = "";
                        signinPresenter.register(scode, phonenumber, email, clientId);
                    } catch (Exception e) {
                        Toast.makeText(Register.this, "请先输入以上信息", Toast.LENGTH_SHORT).show();
                    }
                }

                break;
            case R.id.btn_controller:
                registerDialog = new RegisterDialog(this);
                registerDialog.setTitle("免责协议");
                registerDialog.setMessage(Constant.TAI_TOS);
                registerDialog.setYesOnclickListener("同意", new RegisterDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        cbController.setChecked(true);
                        registerDialog.dismiss();
                    }
                });

                registerDialog.setNoOnclickListener("不同意", new RegisterDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        cbController.setChecked(false);
                        registerDialog.dismiss();
                    }
                });
                registerDialog.show();
                break;
        }
    }

    private String getScode(String s) {
        String scode = null;
        try {
            scode = Constant.mD5Code(s);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return scode;
    }
}
